package yagwl.client.widget;

import java.util.ArrayList;
import java.util.List;

import yagwl.client.resource.Resources;

import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * A containter panel for input widgets. Input widgets are layed out
 * horizontally. Optionally, add text below or as a popup, usually hint
 * messages. Uses method chaining
 * <p>
 * <b>CSS styles</b><br>
 * .ya-input-hint - <br>
 * .ya-input-popup-hint
 */
public class InputWidgetContainer extends Composite {
  private List<Widget> widgetLst = new ArrayList<Widget>();
  private Widget mouseOverHint;
  private Widget displayedHint;
  private VerticalPanel vPanel = new VerticalPanel();
  private HintPopup popup;
  private int positionLeft = 20;
  private int positionTop = -20;

  public InputWidgetContainer(Widget widget) {
    widgetLst.add(widget);
    initWidget(vPanel);
  }

  public InputWidgetContainer addInputWidget(Widget widget) {
    widgetLst.add(widget);
    return this;
  }

  public InputWidgetContainer setMouseOverHint(String mouseOverHint) {
    Label l = new Label(mouseOverHint);
    setMouseOverHint(l);
    return this;
  }

  public InputWidgetContainer setMouseOverHint(Widget mouseOverHint) {
    this.mouseOverHint = mouseOverHint;
    mouseOverHint.setStyleName("ya-input-hint");
    return this;
  }

  public InputWidgetContainer setDisplayedHint(String displayedHint) {
    Label l = new Label(displayedHint);
    setDisplayedHint(l);
    return this;
  }

  public InputWidgetContainer setDisplayedHint(Widget displayedHint) {
    this.displayedHint = displayedHint;
    displayedHint.setStyleName("ya-input-hint");
    return this;
  }

  public void setPositionLeft(int positionLeft) {
    this.positionLeft = positionLeft;
  }

  public void setPositionTop(int positionTop) {
    this.positionTop = positionTop;
  }

  public InputWidgetContainer renderUI() {
    HorizontalPanel hPanel = new HorizontalPanel();
    for (Widget widget : widgetLst)
      hPanel.add(widget);

    if (mouseOverHint != null) {
      hPanel.add(new Spacer(2, 1));
      Image img = new Image(Resources.I.getInlineQuestion());
      hPanel.add(img);
      popup = new HintPopup();
      img.addMouseOverHandler(new HintMouseOverHandler());
      img.addMouseOutHandler(new HintMouseOutHandler());
    }
    vPanel.add(hPanel);

    if (displayedHint != null) {
      vPanel.add(displayedHint);
    }
    return this;
  }

  private class HintMouseOverHandler implements MouseOverHandler {
    @Override
    public void onMouseOver(MouseOverEvent event) {
      Widget sender = (Widget) event.getSource();
      popup.setPopupPosition(sender.getAbsoluteLeft() + positionLeft,
          sender.getAbsoluteTop() + positionTop);
      popup.show();
    }
  }

  private class HintMouseOutHandler implements MouseOutHandler {
    @Override
    public void onMouseOut(MouseOutEvent event) {
      popup.hide();
    }
  }

  private class HintPopup extends PopupPanel {
    public HintPopup() {
      setWidget(mouseOverHint);
      addStyleName("ya-input-popup-hint");
    }
  }
}
